/**
 * Copyright 2009 Wisdom
 */
package com.wisdom.model.entities;

import java.util.Date;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

/**
 * Entity represent of user comment in system
 * 
 * @author alekseiko
 * @since 1.0
 */
@Entity
@Table(name = "WE_COMMENTS")
public class Comment {

    private long id;
    private String comment;
    private Date commentDate;
    private int votes;
    private boolean right;
    private User owner;
    private Post post;
    private Comment parent;
    private Set<Comment> childs;

    /**
     * Default constructor
     */
    public Comment() {
    }

    /**
     * Constructor initialize all fields.
     * 
     * @param id
     *            - unique comment id
     * @param comment
     *            - comment text
     * @param commentDate
     *            - create date
     * @param votes
     *            - user votes
     * @param right
     *            - if this flag true then comment is nice
     * @param owner
     *            - comment owner
     * @param post
     *            - commented post
     * @param parent
     *            - parent comment
     * @param childs
     *            - childs comments
     */
    public Comment(long id, String comment, Date commentDate, int votes,
	    boolean right, User owner, Post post, Comment parent,
	    Set<Comment> childs) {
	this.id = id;
	this.comment = comment;
	this.commentDate = commentDate;
	this.votes = votes;
	this.right = right;
	this.owner = owner;
	this.post = post;
	this.parent = parent;
	this.childs = childs;
    }

    /**
     * Get unique comment id
     * 
     * @return the id
     */
    @Id
    @Column(name = "WE_ID")
    @GeneratedValue(strategy = GenerationType.SEQUENCE)
    public long getId() {
	return id;
    }

    /**
     * Set unuque comment id
     * 
     * @param id
     *            unique comment id
     */
    public void setId(long id) {
	this.id = id;
    }

    /**
     * Get comment text
     * 
     * @return the comment
     */
    @Column(name = "WE_COMMENT", nullable = false, updatable = true)
    public String getComment() {
	return comment;
    }

    /**
     * Set comment text
     * 
     * @param comment
     *            - comment text
     */
    public void setComment(String comment) {
	this.comment = comment;
    }

    /**
     * Get comment create date
     * 
     * @return the commentDate
     */
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "WE_COMMENT_DATE", nullable = false)
    public Date getCommentDate() {
	return commentDate;
    }

    /**
     * Set comment create date.
     * 
     * @param commentDate
     *            - comment create date
     */
    public void setCommentDate(Date commentDate) {
	this.commentDate = commentDate;
    }

    /**
     * Get comment votes
     * 
     * @return the votes
     */
    @Column(name = "WE_VOTES", nullable = false, updatable = true)
    public int getVotes() {
	return votes;
    }

    /**
     * Set comment votes.
     * 
     * @param votes
     *            - user votes
     */
    public void setVotes(int votes) {
	this.votes = votes;
    }

    /**
     * Is right flag
     * 
     * @return the right
     */
    @Column(name = "WE_IS_RIGHT", nullable = false, updatable = true)
    public boolean isRight() {
	return right;
    }

    /**
     * Set right flag.
     * 
     * @param right
     *            - if this flag true then comment is nice
     */
    public void setRight(boolean right) {
	this.right = right;
    }

    /**
     * Get comment owner
     * 
     * @return the owner
     */
    @ManyToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "WE_OWNER_ID")
    public User getOwner() {
	return owner;
    }

    /**
     * Set comment owner
     * 
     * @param owner - comment owner
     *            
     */
    public void setOwner(User owner) {
	this.owner = owner;
    }

    /**
     * Get commented post
     * 
     * @return the post
     */
    @ManyToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "WE_POST_ID")
    public Post getPost() {
	return post;
    }

    /**
     * Set commented post
     * 
     * @param post
     *            - commented post
     */
    public void setPost(Post post) {
	this.post = post;
    }

    /**
     * Get parent comment
     * 
     * @return the parent
     */
    @ManyToOne(cascade = CascadeType.ALL, fetch=FetchType.LAZY)
    @JoinColumn(name = "WE_PARENT_COMMENT_ID")
    public Comment getParent() {
	return parent;
    }

    /**
     * Set parent comment.
     * 
     * @param parent
     *            - comment
     */
    public void setParent(Comment parent) {
	this.parent = parent;
    }

    /**
     * Get childs comments
     * 
     * @return the childs
     */
    @OneToMany(mappedBy = "parent")
    public Set<Comment> getChilds() {
	return childs;
    }

    /**
     * Set childs comments
     * 
     * @param childs
     *            -comment
     */
    public void setChilds(Set<Comment> childs) {
	this.childs = childs;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public int hashCode() {
	final int prime = 31;
	int result = 1;
	result = prime * result + (int) (id ^ (id >>> 32));
	return result;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public boolean equals(Object obj) {
	if (this == obj)
	    return true;
	if (obj == null)
	    return false;
	if (getClass() != obj.getClass())
	    return false;
	Comment other = (Comment) obj;
	if (id != other.id)
	    return false;
	return true;
    }
}
